<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<jsp:useBean id="player" class="pe.lucene.web.myrating.Player" scope="session"/>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.Transaction"%>
<%@page import="pe.lucene.web.myrating.HibernateUtil"%>
<%@page import="pe.lucene.web.myrating.Player"%>
<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%@page import="com.oreilly.servlet.MultipartRequest,
                com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<link rel="stylesheet" type="text/css" href="index.css" />
<title>Update OK</title>
</head>
<meta http-equiv="refresh" content="2;url=../index.jsp">

<!-- MultiPart -->
<%
/*
BufferedReader bufReader = new BufferedReader(new InputStreamReader(request.getInputStream()));

String read = "";

while ((read = bufReader.readLine()) != null) {
	out.print(read + "<hr>");
	

}

bufReader.close();
*/
%>

<!-- MultiPartRequest -->
<%
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
String backnum = request.getParameter("backnum");
String position = request.getParameter("position");
String team = request.getParameter("team");
String fileName = null;

String savePath = "/Users/lucene/Documents/workspace/WebMyRating/WebContent/uploadprofile";
int sizeLimit = 5 * 1024 * 1024; //5MB

try {
	MultipartRequest multi = new MultipartRequest(request, savePath, sizeLimit, new DefaultFileRenamePolicy());
	Enumeration formNames = multi.getFileNames(); //return form name
	String formName = (String) formNames.nextElement();
	fileName = multi.getFilesystemName(formName); // return file name
	
	out.print("origin name:" + multi.getOriginalFileName(formName) );
	
	if (fileName == null) { //file not upload
		out.print("File Not Uploaded");
	} else {
		fileName = new String(fileName.getBytes("8859_1"), "UTF-8");
		
		id = multi.getParameter("id");
		passwd = multi.getParameter("passwd");
		backnum = multi.getParameter("backnum");
		position = multi.getParameter("position");
		team = multi.getParameter("team");
	}
} catch (Exception e) {
	out.println("Exception Occured");
}
%>

<%
	Session hbSession = null;
	Transaction tx = null;
	
	try {
		hbSession = HibernateUtil.currentSession();
		tx = hbSession.beginTransaction();
		
		player.setPasswd(passwd);
		if (backnum != null) {
			player.setBacknum(Integer.parseInt(backnum));
		}
		
		if (position != null) {
			player.setPosition(Integer.parseInt(position));
		}
		
		player.setTeam(team);
		
		if (fileName != null)
			player.setPix("uploadprofile/" + fileName);
		
		hbSession.update(player);
		out.println("Session update<BR> passwd:" + passwd + "/team:" + team);
		
		tx.commit();
		
	} catch(Exception ex) {
		out.println("Exception catch");
		out.println(ex.getMessage());
		if (tx != null) tx.rollback();
	} finally {
		HibernateUtil.closeSession();
		out.println("Finally close session");
	}
%>

</body>
</html> 